package com.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.VO.BattleUserVO;
import com.VO.PowerRankVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.entity.*;
import com.po.BattleBossRedisPO;
import com.po.BattleRedisPO;
import com.po.SkillPO;
import com.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 前端控制器
 *
 * @author LiYuan
 * @since 2021-10-30
 */
@RestController
@RequestMapping("/pokemon")
@CrossOrigin
@Slf4j
public class TalkPokemonUserController {
  @Autowired private ITalkPokemonUserService talkPokemonUserService;
  @Autowired private ITalkBallUserService talkBallUserService;
  @Autowired private ITalkPokemonService talkPokemonService;
  @Autowired private RedisTemplate redisTemplate;
  @Autowired private TalkKiaraService talkKiaraService;
  @Autowired private IPokemonUserDpService pokemonUserDpService;
  @Autowired private TalkRecordsService talkRecordsService;
  @Autowired private IUserCishuService userCishuService;
  @Autowired private ITalkPokemonBossService bossService;
  @Autowired private IFubenService fubenService;
  @Autowired private ISkillOriginService skillOriginService;
  @Autowired private IPokemonUserSkillService userSkillService;
  @Autowired private IBossSkillService bossSkillService;

  class TimeOutTask extends TimerTask {

    @Override
    public void run() {

    }
  }

  @RequestMapping("/getPokemon")
  public ResponseMessage getPokmon(Integer userId) {

    TalkBallUser talkBallUser =
        talkBallUserService.getOne(
            new LambdaQueryWrapper<TalkBallUser>().eq(TalkBallUser::getUserId, userId));
    List<TalkPokemon> talkPokemonList = talkPokemonService.list();
    List<Integer> talkPokemonIds = new ArrayList<>();
    talkPokemonList.forEach(
        s -> {
          if (s.getRankk() == 1) {
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());

          } else if (s.getRankk() == 2) {
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
          } else if (s.getRankk() == 3) {
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
          } else if (s.getRankk() == 4) {
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
            talkPokemonIds.add(s.getId());
          } else {
            talkPokemonIds.add(s.getId());
          }
        });
    if (talkBallUser.getBallNum() > 0) {
      Integer pokemonUserId = talkPokemonIds.get((int) (Math.random() * talkPokemonIds.size()));
      for (TalkPokemon t : talkPokemonList) {
        if (t.getId() == pokemonUserId) {
          int count =
              talkPokemonUserService.count(
                  new LambdaQueryWrapper<TalkPokemonUser>()
                      .eq(TalkPokemonUser::getOwnerId, userId));

          TalkPokemonUser talkPokemonUser = new TalkPokemonUser();
          talkPokemonUser.setPokemonId(t.getId());
          talkPokemonUser.setAtk(t.getAtk());
          talkPokemonUser.setDef(t.getDef());
          talkPokemonUser.setOwnerId(userId);
          talkPokemonUser.setHp(t.getHp());
          talkPokemonUser.setImg(t.getImg());
          talkPokemonUser.setSatk(t.getSatk());
          talkPokemonUser.setSpe(t.getSpe());
          talkPokemonUser.setSdef(t.getSdef());
          talkPokemonUser.setLevel(t.getLevel());
          talkPokemonUser.setShuxin(t.getShuxin());
          talkPokemonUser.setName(t.getName());
          talkPokemonUser.setShuxintwo(t.getShuxintwo() != null ? t.getShuxintwo() : null);
          if (count > 0) {
            talkPokemonUser.setShoww(0);
          } else {
            talkPokemonUser.setShoww(1);
          }

          talkPokemonUserService.save(talkPokemonUser);
          Integer ballNum = talkBallUser.getBallNum() - 1;
          talkBallUser.setBallNum(ballNum);
          talkBallUserService.update(
              talkBallUser,
              new LambdaQueryWrapper<TalkBallUser>().eq(TalkBallUser::getUserId, userId));
          return new ResponseMessage(ResponseMessage.SUCCESS, "成功", t);
        }
      }

    } else {

      return new ResponseMessage(ResponseMessage.FAIL, "没有精灵球了");
    }

    return new ResponseMessage(ResponseMessage.FAIL, "召唤宝可梦失败");
  }

  @RequestMapping("findUserPokemonList")
  public ResponseMessage findUserPokemonList(Integer userId) {

    List<TalkPokemonUser> list =
        talkPokemonUserService.list(
            new LambdaQueryWrapper<TalkPokemonUser>().eq(TalkPokemonUser::getOwnerId, userId));
    return new ResponseMessage(ResponseMessage.SUCCESS, "成功", list);
  }

  @RequestMapping("findUserPokemonInfoById")
  public ResponseMessage findUserPokemonInfoById(Integer id) {

    TalkPokemonUser one =
        talkPokemonUserService.getOne(
            new LambdaQueryWrapper<TalkPokemonUser>().eq(TalkPokemonUser::getId, id));

    return new ResponseMessage(ResponseMessage.SUCCESS, "成功", one);
  }

  @RequestMapping("setShowPokemon")
  public ResponseMessage setShowPokemon(Integer id, Integer userId) {
    UpdateWrapper<TalkPokemonUser> wrapper1 = new UpdateWrapper<>();
    wrapper1.set("showw", 0).eq("showw", 1).eq("owner_id", userId);

    talkPokemonUserService.update(wrapper1);
    UpdateWrapper<TalkPokemonUser> wrapper2 = new UpdateWrapper<>();
    wrapper2.set("showw", 1).eq("id", id);
    boolean update = talkPokemonUserService.update(wrapper2);
    return new ResponseMessage(ResponseMessage.SUCCESS, "成功", update);
  }

  @RequestMapping("findUserPokemonInfoByShow")
  public ResponseMessage findUserPokemonInfoByShow(Integer userId) {

    TalkPokemonUser one =
        talkPokemonUserService.getOne(
            new LambdaQueryWrapper<TalkPokemonUser>()
                .eq(TalkPokemonUser::getShoww, 1)
                .eq(TalkPokemonUser::getOwnerId, userId));

    return new ResponseMessage(ResponseMessage.SUCCESS, "成功", one);
  }

  @RequestMapping("findUserBallNum")
  public ResponseMessage findUserBallNum(Integer userId) {

    TalkBallUser one =
        talkBallUserService.getOne(
            new LambdaQueryWrapper<TalkBallUser>().eq(TalkBallUser::getUserId, userId));

    return new ResponseMessage(ResponseMessage.SUCCESS, "成功", one);
  }

  @RequestMapping("deletePokemonById")
  public ResponseMessage deletePokemonById(Integer id, Integer userId) {

    boolean remove =
        talkPokemonUserService.remove(
            new LambdaQueryWrapper<TalkPokemonUser>()
                .eq(TalkPokemonUser::getId, id)
                .eq(TalkPokemonUser::getOwnerId, userId));

    userSkillService.remove(new LambdaQueryWrapper<PokemonUserSkill>().eq(PokemonUserSkill::getPokeId,id));


    return new ResponseMessage(ResponseMessage.SUCCESS, "成功", remove);
  }
  // 签到，送球
  @RequestMapping("qiandao")
  public ResponseMessage qiandao(Integer userId) {
    if (redisTemplate.hasKey("qdstatus" + userId)) {
      return new ResponseMessage(ResponseMessage.SUCCESS, "宝贝你已经签到过了哦", false);
    } else {
      Date date = new Date();
      SimpleDateFormat dfh = new SimpleDateFormat("HH");//设置日期格式
      String dateh = dfh.format(date);//
      Integer dateNowHH=Integer.parseInt(dateh);
      SimpleDateFormat dfm = new SimpleDateFormat("mm");//设置日期格式
      String datem = dfh.format(date);//
      Integer dateNowMM=Integer.parseInt(datem);
      SimpleDateFormat dfs = new SimpleDateFormat("ss");//设置日期格式
      String dates = dfh.format(date);//
      Integer dateNowSS=Integer.parseInt(dates);

      Integer termTime=60*60*24-dateNowHH*3600-dateNowMM*60-dateNowSS;
      redisTemplate.opsForValue().set("qdstatus" + userId, 200, termTime, TimeUnit.SECONDS);
      log.info(termTime.toString());
    }

    UpdateWrapper wrapper = new UpdateWrapper();
    wrapper.setSql("ball_num = ball_num + 1");
    wrapper.eq("user_id", userId);
    boolean update = talkBallUserService.update(wrapper);

    return new ResponseMessage(ResponseMessage.SUCCESS, "签到成功", true);
  }

  // 战力排行榜
  @RequestMapping("powerRankList")
  public ResponseMessage powerRankList() {
    //		List<TalkPokemonUser> powerListinit = talkPokemonUserService.list(new
    // LambdaQueryWrapper<TalkPokemonUser>().eq(TalkPokemonUser::getShoww, 1));
    List<PowerRankVO> powerRankVOS = talkPokemonUserService.powerRankList();

    return new ResponseMessage(ResponseMessage.SUCCESS, "成功", powerRankVOS);
  }

  // 自动战斗 玩家间
  // 发起战斗
  @RequestMapping("battleStart")
  public ResponseMessage battleStart(Integer userOneId, Integer userTwoId) {
    BattleUserVO userOneInfo = talkPokemonUserService.getBattleUserInfo(userOneId);
    BattleUserVO userTwoInfo = talkPokemonUserService.getBattleUserInfo(userTwoId);
    String userOneName=userOneInfo.getKiaraName();
    String userTwoName=userTwoInfo.getKiaraName();
    //1为战斗中，2为战斗结束
   int status=1;
   //本次战斗的唯一id，用于redis检索战斗状态
   String battleId=UUID.randomUUID().toString().replaceAll("-", "");

    List<String> battleMessageList=new ArrayList<>();
    battleMessageList.add(userOneName+"向"+userTwoName+"发起了挑战");
    battleMessageList.add("第1回合开始");
    TalkPokemonUser one =
        talkPokemonUserService.getOne(
            new LambdaQueryWrapper<TalkPokemonUser>()
                .eq(TalkPokemonUser::getOwnerId, userOneId)
                .eq(TalkPokemonUser::getShoww, 1));
    if (one==null){
      return new ResponseMessage(ResponseMessage.FAIL,"你没有出战的宝可梦");
    }
    TalkPokemonUser two =
            talkPokemonUserService.getOne(
                    new LambdaQueryWrapper<TalkPokemonUser>()
                            .eq(TalkPokemonUser::getOwnerId, userTwoId)
                            .eq(TalkPokemonUser::getShoww, 1));
     //获取双方的技能
    // one的技能
    List<PokemonUserSkill> userSkills1 = userSkillService.list(new LambdaQueryWrapper<PokemonUserSkill>().eq(PokemonUserSkill::getPokeId,one.getId()));
    List<Integer> skillIds = userSkills1.stream().map(s -> s.getSkillId()).collect(Collectors.toList());
    List<SkillOrigin> skillOrigins1 = skillOriginService.list();
    List<SkillOrigin> userSkillListFinally1=new ArrayList<>();
    if (skillIds.size()>0){
      userSkillListFinally1 = skillOrigins1.stream().filter(s -> skillIds.contains(s.getSkillId())).collect(Collectors.toList());
    }else {
      userSkillListFinally1=defSkillList();
    }

    // two的技能
    List<PokemonUserSkill> userSkills2 = userSkillService.list(new LambdaQueryWrapper<PokemonUserSkill>().eq(PokemonUserSkill::getPokeId,two.getId()));
    List<Integer> skillIds2 = userSkills2.stream().map(s -> s.getSkillId()).collect(Collectors.toList());
    List<SkillOrigin> skillOrigins2 = skillOriginService.list();
    List<SkillOrigin> userSkillListFinally2=new ArrayList<>();
    if (skillIds2.size()>0){
      userSkillListFinally2 = skillOrigins2.stream().filter(s -> skillIds2.contains(s.getSkillId())).collect(Collectors.toList());
    }else {
      userSkillListFinally2=defSkillList();
    }


    //战斗过程
   status=battleResultPerTurn(one,two,userOneName,userTwoName,userSkillListFinally1,userSkillListFinally2,battleMessageList,status);

    if (one.getHp()<=0){
      battleMessageList.add(userTwoName+"获得了胜利");
      TalkRecords talkRecords=new TalkRecords();
      Snowflake snowflake = IdUtil.createSnowflake(5, 5);
      long uuid = snowflake.nextId();
      talkRecords.setId(uuid);
      talkRecords.setKiaraId(userOneInfo.getKiaraId());
      talkRecords.setPostTime(new Date());
      talkRecords.setUserId(userOneInfo.getUserId());
      talkRecords.setStatus(2);
      talkRecords.setContent(userOneName+"的"+one.getName()+"向"+userTwoName+"的"+two.getName()+"发起了挑战，结果反手被秒杀");
      talkRecordsService.save(talkRecords);
      //傻逼策划不给东西是
//      Integer getDp=(int)(Math.random()*6000+3000);
//      UpdateWrapper wrapper=new UpdateWrapper();
//      wrapper.set("")
//      pokemonUserDpService.

    }else if (two.getHp()<=0){
      battleMessageList.add(userOneName+"获得了胜利");
      TalkRecords talkRecords=new TalkRecords();
      Snowflake snowflake = IdUtil.createSnowflake(4, 4);
      long uuid = snowflake.nextId();
      talkRecords.setId(uuid);
      talkRecords.setKiaraId(userOneInfo.getKiaraId());
      talkRecords.setPostTime(new Date());
      talkRecords.setUserId(userOneInfo.getUserId());
      talkRecords.setStatus(2);
      talkRecords.setContent(userOneName+"的"+one.getName()+"秒杀了"+userTwoName+"的"+two.getName()+"小垃圾，获得了胜利");
      talkRecordsService.save(talkRecords);
    }



    BattleRedisPO battleRedisPO=new BattleRedisPO();
    battleRedisPO.setStatus(status==2?2:1);
    battleRedisPO.setBattleMessageList(battleMessageList);
    battleRedisPO.setTurnNum(1);
    battleRedisPO.setPokemonOne(one);
    battleRedisPO.setPokemonTwo(two);
    battleRedisPO.setUserOneInfo(userOneInfo);
    battleRedisPO.setUserTwoInfo(userTwoInfo);
    battleRedisPO.setBattleId(battleId);
    JSON parse = JSONUtil.parse(battleRedisPO);
    redisTemplate.opsForValue().set(battleId,parse,30,TimeUnit.MINUTES);


    return new ResponseMessage(ResponseMessage.SUCCESS, "成功", battleRedisPO);
  }


  // 战斗进行
  @RequestMapping("getBattleing")
  public  ResponseMessage getBattleInfo(String battleId){
    Object o = redisTemplate.opsForValue().get(battleId);
    JSONObject jsonObject = JSONUtil.parseObj(o);
    BattleRedisPO battleRedisPO = JSONUtil.toBean(jsonObject, BattleRedisPO.class);
    if (battleRedisPO.getStatus()==2){
      return new ResponseMessage(ResponseMessage.FAIL,"战斗已经结束了笨比");
    }
    List<String> battleMessageList=battleRedisPO.getBattleMessageList();
    battleRedisPO.setTurnNum(battleRedisPO.getTurnNum()+1);
    battleMessageList.add("第"+battleRedisPO.getTurnNum()+"回合开始");

    TalkPokemonUser one =battleRedisPO.getPokemonOne();
    TalkPokemonUser two =battleRedisPO.getPokemonTwo();
    BattleUserVO userOneInfo = battleRedisPO.getUserOneInfo();
    BattleUserVO userTwoInfo = battleRedisPO.getUserTwoInfo();
    String userOneName=userOneInfo.getKiaraName();
    String userTwoName=userTwoInfo.getKiaraName();
    int status = battleRedisPO.getStatus();

    //随机出技能，先写死
    // one的技能
    List<PokemonUserSkill> userSkills1 = userSkillService.list(new LambdaQueryWrapper<PokemonUserSkill>().eq(PokemonUserSkill::getPokeId,one.getId()));
    List<Integer> skillIds = userSkills1.stream().map(s -> s.getSkillId()).collect(Collectors.toList());
    List<SkillOrigin> skillOrigins1 = skillOriginService.list();
    List<SkillOrigin> userSkillListFinally1=new ArrayList<>();
    if (skillIds.size()>0){
      userSkillListFinally1 = skillOrigins1.stream().filter(s -> skillIds.contains(s.getSkillId())).collect(Collectors.toList());
    }else {
      userSkillListFinally1=defSkillList();
    }

    // two的技能
    List<PokemonUserSkill> userSkills2 = userSkillService.list(new LambdaQueryWrapper<PokemonUserSkill>().eq(PokemonUserSkill::getPokeId,two.getId()));
    List<Integer> skillIds2 = userSkills2.stream().map(s -> s.getSkillId()).collect(Collectors.toList());
    List<SkillOrigin> skillOrigins2 = skillOriginService.list();
    List<SkillOrigin> userSkillListFinally2=new ArrayList<>();
    if (skillIds2.size()>0){
      userSkillListFinally2 = skillOrigins2.stream().filter(s -> skillIds2.contains(s.getSkillId())).collect(Collectors.toList());
    }else {
      userSkillListFinally2=defSkillList();
    }
    //战斗过程
     status=battleResultPerTurn(one,two,userOneName,userTwoName,userSkillListFinally1,userSkillListFinally2,battleMessageList,status);

    if (one.getHp()<=0){
      battleMessageList.add(userTwoName+"获得了胜利");

      TalkRecords talkRecords=new TalkRecords();
      Snowflake snowflake = IdUtil.createSnowflake(3, 3);
      long uuid = snowflake.nextId();
      talkRecords.setId(uuid);
      talkRecords.setKiaraId(userOneInfo.getKiaraId());
      talkRecords.setPostTime(new Date());
      talkRecords.setUserId(userOneInfo.getUserId());
      talkRecords.setStatus(2);
      talkRecords.setContent(userOneName+"的"+one.getName()+"向"+userTwoName+"的"+two.getName()+"发起了挑战，结果被打的头破血流");
      talkRecordsService.save(talkRecords);
    }else if (two.getHp()<=0){
      battleMessageList.add(userOneName+"获得了胜利");
      TalkRecords talkRecords=new TalkRecords();
      Snowflake snowflake = IdUtil.createSnowflake(2, 2);
      long uuid = snowflake.nextId();
      talkRecords.setId(uuid);
      talkRecords.setKiaraId(userOneInfo.getKiaraId());
      talkRecords.setPostTime(new Date());
      talkRecords.setUserId(userOneInfo.getUserId());
      talkRecords.setStatus(2);
      talkRecords.setContent(userOneName+"的"+one.getName()+"乱杀了"+userTwoName+"的"+two.getName()+"获得了胜利");
      talkRecordsService.save(talkRecords);
    }

    battleRedisPO.setStatus(status==2?2:1);
    battleRedisPO.setBattleMessageList(battleMessageList);
//    battleRedisPO.setTurnNum(1);
    battleRedisPO.setPokemonOne(one);
    battleRedisPO.setPokemonTwo(two);
    battleRedisPO.setUserOneInfo(userOneInfo);
    battleRedisPO.setUserTwoInfo(userTwoInfo);
    battleRedisPO.setBattleId(battleId);
    JSON parse = JSONUtil.parse(battleRedisPO);
    redisTemplate.opsForValue().set(battleId,parse,30,TimeUnit.MINUTES);




    return new ResponseMessage(ResponseMessage.SUCCESS, "成功", battleRedisPO);
  }
//计算每次伤害结算的双方属性变化
  public void shuxingbianhua(TalkPokemonUser atker,TalkPokemonUser defder,SkillOrigin skillOrigin){
    //atker攻击方，defder挨打方
    atker.setSpe(atker.getSpe()/skillOrigin.getSpeDownSelf());//攻击方速度下降值
    atker.setAtk(atker.getAtk()/skillOrigin.getAtkDownSelf());//攻击方攻击下降值
    atker.setSatk(atker.getSatk()/skillOrigin.getSatkDownSelf());//攻击方特攻下降值
    atker.setDef(atker.getDef()/skillOrigin.getDefDownSelf());//攻击方防御下降值
    atker.setSdef(atker.getSdef()/skillOrigin.getSdefDownSelf());//攻击方特防下降值

    atker.setSpe(atker.getSpe()*skillOrigin.getSpeUpSelf());
    atker.setAtk(atker.getAtk()*skillOrigin.getAtkUpSelf());
    atker.setSatk(atker.getSatk()*skillOrigin.getSatkUpSelf());
    atker.setDef(atker.getDef()*skillOrigin.getDefUpSelf());
    atker.setSdef(atker.getSdef()*skillOrigin.getSdefUpSelf());

    defder.setSpe(defder.getSpe()/skillOrigin.getSpeDownIt());
    defder.setAtk(defder.getAtk()/skillOrigin.getAtkDownIt());
    defder.setSatk(defder.getSatk()/skillOrigin.getSatkDownIt());
    defder.setDef(defder.getDef()/skillOrigin.getDefDownIt());
    defder.setSdef(defder.getSdef()/skillOrigin.getSdefDownIt());
  }
  // 计算每回合的战斗结果 玩家间
  public int battleResultPerTurn(
      TalkPokemonUser one,
      TalkPokemonUser two,
      String userOneName,
      String userTwoName,
      List<SkillOrigin> skillPOList,
      List<SkillOrigin> skillPOList2,
      List<String> battleMessageList,
      int status) {
    if (one.getSpe() >= two.getSpe() && status == 1) {
      // 1打2
      SkillOrigin oneSkill = skillPOList.get((int) (Math.random() * skillPOList.size()));
      if (oneSkill.getType() == 1) {
        Integer damage = (one.getAtk() - two.getDef());
        if (damage <= 0) {
          damage = 1;
          damage=(damage*oneSkill.getPower()<=0?1:damage*oneSkill.getPower());
        }else {
          damage=(damage*oneSkill.getPower()<=0?1:damage*oneSkill.getPower());
        }
        battleMessageList.add(
            userOneName
                + "的"
                + one.getName()
                + "对"
                + userTwoName
                + "的"
                + two.getName()
                + "使用"
                + oneSkill.getSkillName()
                + "造成了"
                + damage
                + "点伤害");
        two.setHp(two.getHp() - damage);
        shuxingbianhua(one,two,oneSkill);
//        one.setSpe(oneSkill.getSpeDownSelf());
      } else if (oneSkill.getType() == 2) {
        Integer damage = oneSkill.getPower()  * (one.getSatk() - two.getSdef());
        if (damage <= 0) {
          damage = 1;
          damage=(damage*oneSkill.getPower()<=0?1:damage*oneSkill.getPower());
        }else {
          damage=(damage*oneSkill.getPower()<=0?1:damage*oneSkill.getPower());
        }
        battleMessageList.add(
            userOneName
                + "的"
                + one.getName()
                + "对"
                + userTwoName
                + "的"
                + two.getName()
                + "使用"
                + oneSkill.getSkillName()
                + "造成了"
                + damage
                + "点伤害");
        two.setHp(two.getHp() - damage);
        shuxingbianhua(one,two,oneSkill);
      }
      if (two.getHp() > 0) {
        // 2打1
        SkillOrigin twoSikll = skillPOList2.get((int) (Math.random() * skillPOList2.size()));
        if (twoSikll.getType() == 1) {
          Integer damage = twoSikll.getPower()  * (two.getAtk() - one.getDef());
          if (damage <= 0) {
            damage = 1;
            damage=(damage*oneSkill.getPower()<=0?1:damage*oneSkill.getPower());
          }else {
            damage=(damage*oneSkill.getPower()<=0?1:damage*oneSkill.getPower());
          }
          battleMessageList.add(
              userTwoName
                  + "的"
                  + two.getName()
                  + "对"
                  + userOneName
                  + "的"
                  + one.getName()
                  + "使用"
                  + twoSikll.getSkillName()
                  + "造成了"
                  + damage
                  + "点伤害");
          one.setHp(one.getHp() - damage);
          shuxingbianhua(two,one,twoSikll);
        } else if (twoSikll.getType() == 2) {
          Integer damage = twoSikll.getPower()  * (two.getSatk() - one.getSdef());
          if (damage <= 0) {
            damage = 1;
            damage=(damage*oneSkill.getPower()<=0?1:damage*oneSkill.getPower());
          }else {
            damage=(damage*oneSkill.getPower()<=0?1:damage*oneSkill.getPower());
          }
          battleMessageList.add(
              userTwoName
                  + "的"
                  + two.getName()
                  + "对"
                  + userOneName
                  + "的"
                  + one.getName()
                  + "使用"
                  + twoSikll.getSkillName()
                  + "造成了"
                  + damage
                  + "点伤害");
          one.setHp(one.getHp() - damage);
          shuxingbianhua(two,one,twoSikll);
        }
        if (one.getHp() <= 0) {
          battleMessageList.add(userOneName + "的" + one.getName() + "倒下了");
          status = 2;
        }

      } else {
        battleMessageList.add(userTwoName + "的" + two.getName() + "倒下了");
        status = 2;
      }

    } else if (one.getSpe() < two.getSpe() && status == 1) {
      // 2打1
      SkillOrigin twoSikll = skillPOList2.get((int) (Math.random() * skillPOList2.size()));
      if (twoSikll.getType() == 1) {
        Integer damage = twoSikll.getPower()  * (two.getAtk() - one.getDef());
        if (damage <= 0) {
          damage = 1;
          damage=(damage*twoSikll.getPower()<=0?1:damage*twoSikll.getPower());
        }else {
          damage=(damage*twoSikll.getPower()<=0?1:damage*twoSikll.getPower());
        }
        battleMessageList.add(
            userTwoName
                + "的"
                + two.getName()
                + "对"
                + userOneName
                + "的"
                + one.getName()
                + "使用"
                + twoSikll.getSkillName()
                + "造成了"
                + damage
                + "点伤害");
        one.setHp(one.getHp() - damage);
        shuxingbianhua(two,one,twoSikll);
      } else if (twoSikll.getType() == 2) {
        Integer damage = twoSikll.getPower()  * (two.getSatk() - one.getSdef());
        if (damage <= 0) {
          damage = 1;
          damage=(damage*twoSikll.getPower()<=0?1:damage*twoSikll.getPower());
        }else {
          damage=(damage*twoSikll.getPower()<=0?1:damage*twoSikll.getPower());
        }
        battleMessageList.add(
            userTwoName
                + "的"
                + two.getName()
                + "对"
                + userOneName
                + "的"
                + one.getName()
                + "使用"
                + twoSikll.getSkillName()
                + "造成了"
                + damage
                + "点伤害");
        one.setHp(one.getHp() - damage);
        shuxingbianhua(two,one,twoSikll);
      }
      if (one.getHp() > 0) {
        // 1打2
        SkillOrigin oneSkill = skillPOList.get((int) (Math.random() * skillPOList.size()));
        if (oneSkill.getType() == 1) {
          Integer damage = oneSkill.getPower()  * (one.getAtk() - two.getDef());
          if (damage <= 0) {
            damage = 1;
            damage=(damage*twoSikll.getPower()<=0?1:damage*twoSikll.getPower());
          }else {
            damage=(damage*twoSikll.getPower()<=0?1:damage*twoSikll.getPower());
          }
          battleMessageList.add(
              userOneName
                  + "的"
                  + one.getName()
                  + "对"
                  + userTwoName
                  + "的"
                  + two.getName()
                  + "使用"
                  + oneSkill.getSkillName()
                  + "造成了"
                  + damage
                  + "点伤害");
          two.setHp(two.getHp() - damage);
          shuxingbianhua(one,two,oneSkill);
        } else if (oneSkill.getType() == 2) {
          Integer damage = oneSkill.getPower()  * (one.getSatk() - two.getSdef());
          if (damage <= 0) {
            damage = 1;
            damage=(damage*twoSikll.getPower()<=0?1:damage*twoSikll.getPower());
          }else {
            damage=(damage*twoSikll.getPower()<=0?1:damage*twoSikll.getPower());
          }
          battleMessageList.add(
              userOneName
                  + "的"
                  + one.getName()
                  + "对"
                  + userTwoName
                  + "的"
                  + two.getName()
                  + "使用"
                  + oneSkill.getSkillName()
                  + "造成了"
                  + damage
                  + "点伤害");
          two.setHp(two.getHp() - damage);
          shuxingbianhua(one,two,oneSkill);
        }
        if (two.getHp() <= 0) {
          battleMessageList.add(userTwoName + "的" + two.getName() + "倒下了");
          status = 2;
        }
      } else {
        battleMessageList.add(userOneName + "的" + one.getName() + "倒下了");
        status = 2;
      }
    }
    return status;
}
  //没有技能的默认技能组
  public  List<SkillOrigin> defSkillList(){
    //随机出技能，先写死
    List<SkillOrigin> defSkillList=new ArrayList<>();
    SkillOrigin skillOrigin1=new SkillOrigin(99998,"物理平A",1,1,"一般","废物平A",1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
    SkillOrigin skillOrigin2=new SkillOrigin(99999,"特殊平A",1,2,"一般","废物平A",1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
    defSkillList.add(skillOrigin1);
    defSkillList.add(skillOrigin2);
    return defSkillList;
  }
  //副本Boss的默认技能组,,大吾他应该能用钢爪，算了后续再设计Boss技能表，先写死成平A
  public List<SkillOrigin> bossSkillList(){
    List<SkillOrigin> defSkillList=new ArrayList<>();
    SkillOrigin skillOrigin1=new SkillOrigin(99997,"物理平A",2,1,"一般","boss平A",1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
    SkillOrigin skillOrigin2=new SkillOrigin(99996,"特殊平A",2,2,"一般","boss平A",1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
    defSkillList.add(skillOrigin1);
    defSkillList.add(skillOrigin2);
    return defSkillList;
  }


  //计算每次伤害结算的双方属性变化,boss挨打
  public void shuxingbianhuaBossDefder(TalkPokemonUser atker,TalkPokemonBoss defder,SkillOrigin skillOrigin){
    //atker攻击方，defder挨打方
    atker.setSpe(atker.getSpe()/skillOrigin.getSpeDownSelf());//攻击方速度下降值
    atker.setAtk(atker.getAtk()/skillOrigin.getAtkDownSelf());//攻击方攻击下降值
    atker.setSatk(atker.getSatk()/skillOrigin.getSatkDownSelf());//攻击方特攻下降值
    atker.setDef(atker.getDef()/skillOrigin.getDefDownSelf());//攻击方防御下降值
    atker.setSdef(atker.getSdef()/skillOrigin.getSdefDownSelf());//攻击方特防下降值

    atker.setSpe(atker.getSpe()*skillOrigin.getSpeUpSelf());
    atker.setAtk(atker.getAtk()*skillOrigin.getAtkUpSelf());
    atker.setSatk(atker.getSatk()*skillOrigin.getSatkUpSelf());
    atker.setDef(atker.getDef()*skillOrigin.getDefUpSelf());
    atker.setSdef(atker.getSdef()*skillOrigin.getSdefUpSelf());

    defder.setSpe(defder.getSpe()/skillOrigin.getSpeDownIt());
    defder.setAtk(defder.getAtk()/skillOrigin.getAtkDownIt());
    defder.setSatk(defder.getSatk()/skillOrigin.getSatkDownIt());
    defder.setDef(defder.getDef()/skillOrigin.getDefDownIt());
    defder.setSdef(defder.getSdef()/skillOrigin.getSdefDownIt());
  }
  //计算每次伤害结算的双方属性变化,boss不挨打
  public void shuxingbianhuaBossAtker(TalkPokemonBoss atker,TalkPokemonUser defder,SkillOrigin skillOrigin){
    //atker攻击方，defder挨打方
    atker.setSpe(atker.getSpe()/skillOrigin.getSpeDownSelf());//攻击方速度下降值
    atker.setAtk(atker.getAtk()/skillOrigin.getAtkDownSelf());//攻击方攻击下降值
    atker.setSatk(atker.getSatk()/skillOrigin.getSatkDownSelf());//攻击方特攻下降值
    atker.setDef(atker.getDef()/skillOrigin.getDefDownSelf());//攻击方防御下降值
    atker.setSdef(atker.getSdef()/skillOrigin.getSdefDownSelf());//攻击方特防下降值

    atker.setSpe(atker.getSpe()*skillOrigin.getSpeUpSelf());
    atker.setAtk(atker.getAtk()*skillOrigin.getAtkUpSelf());
    atker.setSatk(atker.getSatk()*skillOrigin.getSatkUpSelf());
    atker.setDef(atker.getDef()*skillOrigin.getDefUpSelf());
    atker.setSdef(atker.getSdef()*skillOrigin.getSdefUpSelf());

    defder.setSpe(defder.getSpe()/skillOrigin.getSpeDownIt());
    defder.setAtk(defder.getAtk()/skillOrigin.getAtkDownIt());
    defder.setSatk(defder.getSatk()/skillOrigin.getSatkDownIt());
    defder.setDef(defder.getDef()/skillOrigin.getDefDownIt());
    defder.setSdef(defder.getSdef()/skillOrigin.getSdefDownIt());
  }

  //战斗过程，副本boss 替换two
  public int battleResultPerTurnBoss(
          TalkPokemonUser one,
          TalkPokemonBoss two,
          String userOneName,
          String userTwoName,
          List<SkillOrigin> skillPOList,
          List<SkillOrigin> skillPOList2,
          List<String> battleMessageList,
          Integer status) {
    if (one.getSpe() >= two.getSpe() && status == 1) {
      // 1打2
      SkillOrigin oneSkill = skillPOList.get((int) (Math.random() * skillPOList.size()));
      if (oneSkill.getType() == 1) {
        Integer damage = oneSkill.getPower()  * (one.getAtk() - two.getDef());
        if (damage <= 0) {
          damage = 1;
        }
        battleMessageList.add(
                userOneName
                        + "的"
                        + one.getName()
                        + "对"
                        + userTwoName
                        + "的"
                        + two.getName()
                        + "使用"
                        + oneSkill.getSkillName()
                        + "造成了"
                        + damage
                        + "点伤害");
        two.setHp(two.getHp() - damage);
        shuxingbianhuaBossDefder(one,two,oneSkill);
      } else if (oneSkill.getType() == 2) {
        Integer damage = oneSkill.getPower()  * (one.getSatk() - two.getSdef());
        if (damage <= 0) {
          damage = 1;
        }
        battleMessageList.add(
                userOneName
                        + "的"
                        + one.getName()
                        + "对"
                        + userTwoName
                        + "的"
                        + two.getName()
                        + "使用"
                        + oneSkill.getSkillName()
                        + "造成了"
                        + damage
                        + "点伤害");
        two.setHp(two.getHp() - damage);
        shuxingbianhuaBossDefder(one,two,oneSkill);
      }
      if (two.getHp() > 0) {
        // 2打1
        SkillOrigin twoSikll = skillPOList2.get((int) (Math.random() * skillPOList2.size()));
        if (twoSikll.getType() == 1) {
          Integer damage = twoSikll.getPower()  * (two.getAtk() - one.getDef());
          if (damage <= 0) {
            damage = 1;
          }
          battleMessageList.add(
                  userTwoName
                          + "的"
                          + two.getName()
                          + "对"
                          + userOneName
                          + "的"
                          + one.getName()
                          + "使用"
                          + twoSikll.getSkillName()
                          + "造成了"
                          + damage
                          + "点伤害");
          one.setHp(one.getHp() - damage);
          shuxingbianhuaBossAtker(two,one,twoSikll);
        } else if (twoSikll.getType() == 2) {
          Integer damage = twoSikll.getPower()  * (two.getSatk() - one.getSdef());
          if (damage <= 0) {
            damage = 1;
          }
          battleMessageList.add(
                  userTwoName
                          + "的"
                          + two.getName()
                          + "对"
                          + userOneName
                          + "的"
                          + one.getName()
                          + "使用"
                          + twoSikll.getSkillName()
                          + "造成了"
                          + damage
                          + "点伤害");
          one.setHp(one.getHp() - damage);
          shuxingbianhuaBossAtker(two,one,twoSikll);
        }
        if (one.getHp() <= 0) {
          battleMessageList.add(userOneName + "的" + one.getName() + "倒下了");
          status = 2;
        }

      } else {
        battleMessageList.add(userTwoName + "的" + two.getName() + "倒下了");
        status = 2;
      }

    } else if (one.getSpe() < two.getSpe() && status == 1) {
      // 2打1
      SkillOrigin twoSikll = skillPOList2.get((int) (Math.random() * skillPOList2.size()));
      if (twoSikll.getType() == 1) {
        Integer damage = twoSikll.getPower()  * (two.getAtk() - one.getDef());
        if (damage <= 0) {
          damage = 1;
        }
        battleMessageList.add(
                userTwoName
                        + "的"
                        + two.getName()
                        + "对"
                        + userOneName
                        + "的"
                        + one.getName()
                        + "使用"
                        + twoSikll.getSkillName()
                        + "造成了"
                        + damage
                        + "点伤害");
        one.setHp(one.getHp() - damage);
        shuxingbianhuaBossAtker(two,one,twoSikll);
      } else if (twoSikll.getType() == 2) {
        Integer damage = twoSikll.getPower()  * (two.getSatk() - one.getSdef());
        if (damage <= 0) {
          damage = 1;
        }
        battleMessageList.add(
                userTwoName
                        + "的"
                        + two.getName()
                        + "对"
                        + userOneName
                        + "的"
                        + one.getName()
                        + "使用"
                        + twoSikll.getSkillName()
                        + "造成了"
                        + damage
                        + "点伤害");
        one.setHp(one.getHp() - damage);
        shuxingbianhuaBossAtker(two,one,twoSikll);
      }
      if (one.getHp() > 0) {
        // 1打2
        SkillOrigin oneSkill = skillPOList.get((int) (Math.random() * skillPOList.size()));
        if (oneSkill.getType() == 1) {
          Integer damage = oneSkill.getPower()  * (one.getAtk() - two.getDef());
          if (damage <= 0) {
            damage = 1;
          }
          battleMessageList.add(
                  userOneName
                          + "的"
                          + one.getName()
                          + "对"
                          + userTwoName
                          + "的"
                          + two.getName()
                          + "使用"
                          + oneSkill.getSkillName()
                          + "造成了"
                          + damage
                          + "点伤害");
          two.setHp(two.getHp() - damage);
          shuxingbianhuaBossDefder(one,two,oneSkill);
        } else if (oneSkill.getType() == 2) {
          Integer damage = oneSkill.getPower()  * (one.getSatk() - two.getSdef());
          if (damage <= 0) {
            damage = 1;
          }
          battleMessageList.add(
                  userOneName
                          + "的"
                          + one.getName()
                          + "对"
                          + userTwoName
                          + "的"
                          + two.getName()
                          + "使用"
                          + oneSkill.getSkillName()
                          + "造成了"
                          + damage
                          + "点伤害");
          two.setHp(two.getHp() - damage);
          shuxingbianhuaBossDefder(one,two,oneSkill);
        }
        if (two.getHp() <= 0) {
          battleMessageList.add(userTwoName + "的" + two.getName() + "倒下了");
          status = 2;
        }
      } else {
        battleMessageList.add(userOneName + "的" + one.getName() + "倒下了");
        status = 2;
      }
    }
    return status;
  }


  //副本boss列表
  @RequestMapping("findFuBenBossList")
  public ResponseMessage findFuBenBossList(Integer level){

    List<TalkPokemonBoss> talkPokemonBosses = bossService.list(new LambdaQueryWrapper<TalkPokemonBoss>().eq(level!=null,TalkPokemonBoss::getBossLevel, level));

    return new ResponseMessage(ResponseMessage.SUCCESS,"成功",talkPokemonBosses);
  }

  //副本boss战斗
  @RequestMapping("bossBattle")
  public ResponseMessage bossBattle(Integer userId,Integer bossId,@RequestParam(required = false,defaultValue = "",value = "battleId") String battleId){
    if ("".equals(battleId)){
      BattleUserVO userOneInfo = talkPokemonUserService.getBattleUserInfo(userId);
      String userOneName=userOneInfo.getKiaraName();
      UserCishu cishuServiceOne = userCishuService.getOne(new LambdaQueryWrapper<UserCishu>().eq(UserCishu::getUserId, userId));
      if(cishuServiceOne==null||cishuServiceOne.getCishu()<=0){
        return new ResponseMessage(ResponseMessage.FAIL,"你没有次数,亲");
      }

      TalkPokemonBoss two = bossService.getOne(new LambdaQueryWrapper<TalkPokemonBoss>().eq(TalkPokemonBoss::getBossId, bossId));
      String userTwoName=two.getBossName();
      //1为战斗中，2为战斗结束
      int status=1;
      //本次战斗的唯一id，用于redis检索战斗状态
    battleId=UUID.randomUUID().toString().replaceAll("-", "");

      List<String> battleMessageList=new ArrayList<>();
      battleMessageList.add(userOneName+"向"+userTwoName+"发起了挑战");
      battleMessageList.add("第1回合开始");
      TalkPokemonUser one =
              talkPokemonUserService.getOne(
                      new LambdaQueryWrapper<TalkPokemonUser>()
                              .eq(TalkPokemonUser::getOwnerId, userId)
                              .eq(TalkPokemonUser::getShoww, 1));
      if (one==null){
        return new ResponseMessage(ResponseMessage.FAIL,"你没有出战的宝可梦");
      }
      //获取双方的技能
      // one的技能
      List<PokemonUserSkill> userSkills1 = userSkillService.list(new LambdaQueryWrapper<PokemonUserSkill>().eq(PokemonUserSkill::getPokeId,one.getId()));
      List<Integer> skillIds = userSkills1.stream().map(s -> s.getSkillId()).collect(Collectors.toList());
      List<SkillOrigin> skillOrigins1 = skillOriginService.list();
      List<SkillOrigin> userSkillListFinally1=new ArrayList<>();
      if (skillIds.size()>0){
        userSkillListFinally1 = skillOrigins1.stream().filter(s -> skillIds.contains(s.getSkillId())).collect(Collectors.toList());
      }else {
        userSkillListFinally1=defSkillList();
      }

      // two的技能
      List<BossSkill> userSkills2 = bossSkillService.list(new LambdaQueryWrapper<BossSkill>().eq(BossSkill::getBossId,two.getBossId()));
      List<Integer> skillIds2 = userSkills2.stream().map(s -> s.getSkillId()).collect(Collectors.toList());
      List<SkillOrigin> skillOrigins2 = skillOriginService.list();
      List<SkillOrigin> userSkillListFinally2=new ArrayList<>();
      if (skillIds2.size()>0){
        userSkillListFinally2 = skillOrigins2.stream().filter(s -> skillIds2.contains(s.getSkillId())).collect(Collectors.toList());
      }else {
        userSkillListFinally2=bossSkillList();
      }


      //战斗过程
      status=battleResultPerTurnBoss(one,two,userOneName,userTwoName,userSkillListFinally1,userSkillListFinally2,battleMessageList,status);

      if (one.getHp()<=0){
        battleMessageList.add(userTwoName+"获得了胜利");
        battleMessageList.add("挑战副本boss失败");



      }else if (two.getHp()<=0){
        battleMessageList.add(userOneName+"获得了胜利");
        Integer victoryDp = two.getVictoryDp();
        battleMessageList.add("挑战副本boss成功,获得了"+victoryDp+"点DP");
        //副本战斗奖励
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.setSql("dp=dp+"+victoryDp);
        wrapper.eq("user_id",userOneInfo.getUserId());
        pokemonUserDpService.update(wrapper);
      }

      UpdateWrapper wrapper=new UpdateWrapper();
      wrapper.setSql("cishu=cishu-1");
      wrapper.eq("user_id",userId);
      userCishuService.update(wrapper);

      BattleBossRedisPO battleRedisPO=new BattleBossRedisPO();
      battleRedisPO.setStatus(status==2?2:1);
      battleRedisPO.setBattleMessageList(battleMessageList);
      battleRedisPO.setTurnNum(1);
      battleRedisPO.setPokemonOne(one);
      battleRedisPO.setPokemonTwo(two);
      battleRedisPO.setUserOneInfo(userOneInfo);
      battleRedisPO.setBattleId(battleId);
      JSON parse = JSONUtil.parse(battleRedisPO);
      redisTemplate.opsForValue().set(battleId,parse,30,TimeUnit.MINUTES);
      return new ResponseMessage(ResponseMessage.SUCCESS, "成功", battleRedisPO);
    }else {
      //后续回合战斗
      Object o = redisTemplate.opsForValue().get(battleId);
      JSONObject jsonObject = JSONUtil.parseObj(o);
      BattleBossRedisPO battleRedisPO = JSONUtil.toBean(jsonObject, BattleBossRedisPO.class);
      if (battleRedisPO.getStatus()==2){
        return new ResponseMessage(ResponseMessage.FAIL,"战斗已经结束了笨比");
      }
      List<String> battleMessageList=battleRedisPO.getBattleMessageList();
      battleRedisPO.setTurnNum(battleRedisPO.getTurnNum()+1);
      battleMessageList.add("第"+battleRedisPO.getTurnNum()+"回合开始");

      TalkPokemonUser one =battleRedisPO.getPokemonOne();
      TalkPokemonBoss two =battleRedisPO.getPokemonTwo();
      BattleUserVO userOneInfo = battleRedisPO.getUserOneInfo();
      String userOneName=userOneInfo.getKiaraName();
      String userTwoName=two.getBossName();
      int status = battleRedisPO.getStatus();

      //随机出技能，先写死
      //获取双方的技能
      // one的技能
      List<PokemonUserSkill> userSkills1 = userSkillService.list(new LambdaQueryWrapper<PokemonUserSkill>().eq(PokemonUserSkill::getPokeId,one.getId()));
      List<Integer> skillIds = userSkills1.stream().map(s -> s.getSkillId()).collect(Collectors.toList());
      List<SkillOrigin> skillOrigins1 = skillOriginService.list();
      List<SkillOrigin> userSkillListFinally1=new ArrayList<>();
      if (skillIds.size()>0){
        userSkillListFinally1 = skillOrigins1.stream().filter(s -> skillIds.contains(s.getSkillId())).collect(Collectors.toList());
      }else {
        userSkillListFinally1=defSkillList();
      }

      // two的技能
      List<BossSkill> userSkills2 = bossSkillService.list(new LambdaQueryWrapper<BossSkill>().eq(BossSkill::getBossId,two.getBossId()));
      List<Integer> skillIds2 = userSkills2.stream().map(s -> s.getSkillId()).collect(Collectors.toList());
      List<SkillOrigin> skillOrigins2 = skillOriginService.list();
      List<SkillOrigin> userSkillListFinally2=new ArrayList<>();
      if (skillIds2.size()>0){
        userSkillListFinally2 = skillOrigins2.stream().filter(s -> skillIds2.contains(s.getSkillId())).collect(Collectors.toList());
      }else {
        userSkillListFinally2=bossSkillList();
      }
      //战斗过程
      status=battleResultPerTurnBoss(one,two,userOneName,userTwoName,userSkillListFinally1,userSkillListFinally2,battleMessageList,status);

      if (one.getHp()<=0){
        battleMessageList.add(userTwoName+"获得了胜利");
        battleMessageList.add("挑战副本boss失败");

      }else if (two.getHp()<=0){
        battleMessageList.add(userOneName+"获得了胜利");
        Integer victoryDp = two.getVictoryDp();
        battleMessageList.add("挑战副本boss成功,获得了"+victoryDp+"点DP");
        //副本战斗奖励
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.setSql("dp=dp+"+victoryDp);
        wrapper.eq("user_id",userOneInfo.getUserId());
        pokemonUserDpService.update(wrapper);
      }

      battleRedisPO.setStatus(status==2?2:1);
      battleRedisPO.setBattleMessageList(battleMessageList);
//    battleRedisPO.setTurnNum(1);
      battleRedisPO.setPokemonOne(one);
      battleRedisPO.setPokemonTwo(two);
      battleRedisPO.setUserOneInfo(userOneInfo);
      battleRedisPO.setBattleId(battleId);
      JSON parse = JSONUtil.parse(battleRedisPO);
      redisTemplate.opsForValue().set(battleId,parse,30,TimeUnit.MINUTES);
      return new ResponseMessage(ResponseMessage.SUCCESS, "成功", battleRedisPO);
    }


  }


  //副本列表
  @RequestMapping("getFubenList")
  public ResponseMessage getFubenList(){

    List<Fuben> list = fubenService.list();
    return new ResponseMessage(ResponseMessage.SUCCESS,"成功",list);
  }
}


